home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.1 / Examples1 / intuition / nlmenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  8.0 KB  |  351 lines

  1. /*
  2.  * nlmenu.c - shows use of NewLook menus using Intuition and GadTools
  3.  *
  4.  * (c) Copyright 1992-1999 Amiga, Inc.  All rights reserved.
  5.  *
  6.  * This software is provided as-is and is subject to change; no warranties
  7.  * are made.  All use is at your own risk.  No liability or responsibility
  8.  * is assumed.
  9.  *
  10.  * Demo shows off the new look menu features of V39.
  11.  */
  12.  
  13. #include <exec/types.h>
  14. #include <intuition/intuitionbase.h>
  15. #include <intuition/gadgetclass.h>
  16. #include <intuition/imageclass.h>
  17. #include <libraries/gadtools.h>
  18. #include <graphics/gfxbase.h>
  19.  
  20. #include <clib/exec_protos.h>
  21. #include <clib/graphics_protos.h>
  22. #include <clib/intuition_protos.h>
  23. #include <clib/diskfont_protos.h>
  24. #include <clib/gadtools_protos.h>
  25. #include <clib/dos_protos.h>
  26.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29.  
  30. /*------------------------------------------------------------------------*/
  31.  
  32. void main(int, char *[]);
  33. void bail_out(int);
  34. BOOL HandleMenuEvent(UWORD);
  35.  
  36. /*------------------------------------------------------------------------*/
  37.  
  38. /* Here we specify what we want our menus to contain: */
  39.  
  40. struct NewMenu mynewmenu[] =
  41. {
  42.     { NM_TITLE, "Project",      0 , 0, 0, 0,},
  43.     {  NM_ITEM, "Open...",     "O", 0, 0, 0,},
  44.     {  NM_ITEM, "Save",      0 , 0, 0, 0,},
  45.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  46.     {  NM_ITEM, "Print",      0 , 0, 0, 0,},
  47.     {   NM_SUB, "Draft",      0 , CHECKIT|CHECKED, ~1, 0,},
  48.     {   NM_SUB, "NLQ",      0 , CHECKIT, ~2, 0,},
  49.     {   NM_SUB, "Laser",      0 , CHECKIT, ~4, 0,},
  50.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  51.     {  NM_ITEM, "Quit...",     "Q", 0, 0, 0,},
  52.  
  53.     { NM_TITLE, "Edit",      0 , 0, 0, 0,},
  54.     {  NM_ITEM, "Cut",     "X", 0, 0, 0,},
  55.     {  NM_ITEM, "Copy",     "C", 0, 0, 0,},
  56.     {  NM_ITEM, "Paste",     "V", 0, 0, 0,},
  57.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  58.     {  NM_ITEM, "Undo",     "Z", 0, 0, 0,},
  59.  
  60.     {   NM_END, 0,          0 , 0, 0, 0,},
  61. };
  62.  
  63. /*------------------------------------------------------------------------*/
  64.  
  65. struct TextAttr customtattr;
  66. struct TextAttr *tattr;
  67.  
  68. /*------------------------------------------------------------------------*/
  69.  
  70. struct GfxBase *GfxBase = NULL;
  71. struct IntuitionBase *IntuitionBase = NULL;
  72. struct Library *GadToolsBase = NULL;
  73. struct Library *DiskfontBase = NULL;
  74. struct Screen *mysc = NULL;
  75. struct Menu *menu = NULL;
  76. struct Window *mywin = NULL;
  77. struct TextFont *customfont = NULL;
  78. void *vi = NULL;
  79. struct DrawInfo *dri = NULL;
  80. struct Image *checkimage = NULL;
  81. struct Image *amigakeyimage = NULL;
  82.  
  83. /*------------------------------------------------------------------------*/
  84.  
  85. BOOL terminated;
  86.  
  87. /*------------------------------------------------------------------------*/
  88.  
  89. void main(argc, argv)
  90.  
  91. int argc;
  92. char *argv[];
  93.  
  94. {
  95.     struct IntuiMessage *imsg;
  96.     ULONG imsgClass;
  97.     UWORD imsgCode;
  98.     struct TagItem moretags[3];
  99.  
  100.     terminated = FALSE;
  101.  
  102.     if (argc == 2)
  103.     {
  104.     printf("Usage:\n\tnlmenu\nor\n\tnlmenu fontname.font fontsize\n");
  105.     printf("Example:\n\tnlmenu courier.font 15\n");
  106.     bail_out(0);
  107.     }
  108.     /* Open all libraries: */
  109.  
  110.     if (!(GfxBase = (struct GfxBase *)
  111.     OpenLibrary("graphics.library", 39L)))
  112.     bail_out(20);
  113.  
  114.     if (!(IntuitionBase = (struct IntuitionBase *)
  115.     OpenLibrary("intuition.library", 39L)))
  116.     bail_out(20);
  117.  
  118.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 39L)))
  119.     bail_out(20);
  120.  
  121.     if (!(DiskfontBase = OpenLibrary("diskfont.library", 37L)))
  122.     bail_out(20);
  123.  
  124.     if (!(mysc = LockPubScreen(NULL)))
  125.     bail_out(20);
  126.  
  127.     if (!(vi = GetVisualInfo(mysc,
  128.     TAG_DONE)))
  129.     bail_out(20);
  130.  
  131.     if (!(dri = GetScreenDrawInfo(mysc)))
  132.     bail_out(20);
  133.  
  134.     if (argc < 3)
  135.     {
  136.     /* Default to screen's font */
  137.     tattr = mysc->Font;
  138.     }
  139.     else
  140.     {
  141.     LONG longval;
  142.  
  143.     customtattr.ta_Style = 0;
  144.     customtattr.ta_Flags = 0;
  145.     /* Attempt to use the font specified on the command line: */
  146.     customtattr.ta_Name = argv[1];
  147.     /* Convert decimal size to long */
  148.     StrToLong(argv[2], &longval);
  149.     customtattr.ta_YSize = longval;
  150.     tattr = &customtattr;
  151.     if (!(customfont = OpenDiskFont(tattr)))
  152.     {
  153.         printf("Could not open font %s %ld\n", customtattr.ta_Name,
  154.         customtattr.ta_YSize);
  155.         bail_out(20);
  156.     }
  157.  
  158.     /* Generate a custom checkmark whose size matches
  159.      * our custom font
  160.      */
  161.     if (!( checkimage = NewObject(NULL, "sysiclass",
  162.         SYSIA_DrawInfo, dri,
  163.         SYSIA_Which, MENUCHECK,
  164.         SYSIA_ReferenceFont, customfont, /* If NULL, uses dri_Font */
  165.         TAG_DONE) ))
  166.     {
  167.         bail_out(20);
  168.     }
  169.  
  170.     /* Generate a custom Amiga-key image whose size matches
  171.      * our custom font
  172.      */
  173.     if (!( amigakeyimage = NewObject(NULL, "sysiclass",
  174.         SYSIA_DrawInfo, dri,
  175.         SYSIA_Which, AMIGAKEY,
  176.         SYSIA_ReferenceFont, customfont, /* If NULL, uses dri_Font */
  177.         TAG_DONE) ))
  178.     {
  179.         bail_out(20);
  180.     }
  181.     }
  182.  
  183.     /* Build and layout menus using the right font: */
  184.     if (!(menu = CreateMenus(mynewmenu,
  185.     TAG_DONE)))
  186.     {
  187.     bail_out(20);
  188.     }
  189.  
  190.     /* These are only necessary if a custom font was supplied... */
  191.     moretags[0].ti_Tag = GTMN_Checkmark;
  192.     moretags[0].ti_Data = (ULONG) checkimage;
  193.     moretags[1].ti_Tag = GTMN_AmigaKey;
  194.     moretags[1].ti_Data = (ULONG) amigakeyimage;
  195.     moretags[2].ti_Tag = TAG_DONE;
  196.  
  197.     if (!LayoutMenus(menu, vi,
  198.     GTMN_TextAttr, tattr,
  199.     GTMN_NewLookMenus, TRUE,
  200.     (customfont ? TAG_MORE : TAG_DONE), moretags))
  201.     bail_out(20);
  202.  
  203.     /* These are only necessary if a custom font was supplied...
  204.      * Note: we re-use some of the tag-array initializations from above
  205.      */
  206.     moretags[0].ti_Tag = WA_Checkmark;
  207.     moretags[1].ti_Tag = WA_AmigaKey;
  208.  
  209.     if (!(mywin = OpenWindowTags(NULL,
  210.     WA_Width, 500,
  211.     WA_InnerHeight, 100,
  212.     WA_Top, 50,
  213.  
  214.     WA_Activate, TRUE,
  215.     WA_DragBar, TRUE,
  216.     WA_DepthGadget, TRUE,
  217.     WA_CloseGadget, TRUE,
  218.     WA_SizeGadget, TRUE,
  219.     WA_SmartRefresh, TRUE,
  220.  
  221.     /* NOTE: NOCAREREFRESH is not allowed if you use GadTools Gadgets! */
  222.     WA_NoCareRefresh, TRUE,
  223.  
  224.     WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_MENUPICK,
  225.  
  226.     WA_MinWidth, 50,
  227.     WA_MinHeight, 50,
  228.     WA_Title, "GadTools Menu Demo",
  229.     WA_NewLookMenus, TRUE,
  230.     (customfont ? TAG_MORE : TAG_DONE), moretags)))
  231.     bail_out(20);
  232.  
  233.     SetMenuStrip(mywin, menu);
  234.  
  235.     while (!terminated)
  236.     {
  237.     Wait (1 << mywin->UserPort->mp_SigBit);
  238.     /* NOTE:  If you use GadTools gadgets, you must use GT_GetIMsg()
  239.      * and GT_ReplyIMsg() instead of GetMsg() and ReplyMsg().
  240.      * Regular GetMsg() and ReplyMsg() are safe if the only part
  241.      * of GadTools you use are menus...
  242.      */
  243.     while ((!terminated) &&
  244.         (imsg = (struct IntuiMessage *)GetMsg(mywin->UserPort)))
  245.     {
  246.         imsgClass = imsg->Class;
  247.         imsgCode = imsg->Code;
  248.         ReplyMsg((struct Message *)imsg);
  249.         switch (imsgClass)
  250.         {
  251.         case IDCMP_MENUPICK:
  252.             terminated = HandleMenuEvent(imsgCode);
  253.             break;
  254.  
  255.         case IDCMP_CLOSEWINDOW:
  256.             printf("CLOSEWINDOW.\n");
  257.             terminated = TRUE;
  258.             break;
  259.         }
  260.     }
  261.     }
  262.     bail_out(0);
  263. }
  264.  
  265. /*------------------------------------------------------------------------*/
  266.  
  267. /*/ bail_out()
  268.  *
  269.  * Function to close down or free any opened or allocated stuff, and then
  270.  * exit.
  271.  *
  272.  */
  273.  
  274. void bail_out(code)
  275.  
  276. int code;
  277.  
  278. {
  279.     if (mywin)
  280.     {
  281.     ClearMenuStrip(mywin);
  282.     CloseWindow(mywin);
  283.     }
  284.  
  285.     /* None of these two calls mind a NULL parameter, so it's not
  286.      * necessary to check for non-NULL before calling.  If we do that,
  287.      * we must be certain that the OpenLibrary() of GadTools succeeded,
  288.      * or else we would be jumping into outer space:
  289.      */
  290.     if (GadToolsBase)
  291.     {
  292.     FreeMenus(menu);
  293.     FreeVisualInfo(vi);
  294.     CloseLibrary(GadToolsBase);
  295.     }
  296.  
  297.     if (dri)
  298.     {
  299.     FreeScreenDrawInfo( mysc, dri );
  300.     }
  301.  
  302.     if (customfont)
  303.     {
  304.     DisposeObject( amigakeyimage );
  305.     DisposeObject( checkimage );
  306.     CloseFont(customfont);
  307.     }
  308.  
  309.     if (mysc)
  310.     {
  311.     UnlockPubScreen(NULL, mysc);
  312.     }
  313.  
  314.     if (DiskfontBase)
  315.     {
  316.     CloseLibrary(DiskfontBase);
  317.     }
  318.  
  319.     if (IntuitionBase)
  320.     {
  321.     CloseLibrary(IntuitionBase);
  322.     }
  323.  
  324.     if (GfxBase)
  325.     {
  326.     CloseLibrary(GfxBase);
  327.     }
  328.  
  329.     exit(code);
  330. }
  331.  
  332.  
  333. /*------------------------------------------------------------------------*/
  334.  
  335. /*/ HandleMenuEvent()
  336.  *
  337.  * This function handles IntuiMessage events of type MENUPICK.
  338.  *
  339.  */
  340.  
  341. BOOL HandleMenuEvent(code)
  342.  
  343. UWORD code;
  344.  
  345. {
  346.     /* Your code goes here */
  347.     return(FALSE);
  348. }
  349.  
  350. /*------------------------------------------------------------------------*/
  351.